const codeImg = document.querySelector('.verify')
const codeImgs=[
    './img/login-code1.jpg',
    './img/login-code2.jpg',
    './img/login-code3.jpg',
    './img/login-code4.jpg',
    './img/login-code5.jpg'
]
const codeNum=[
    'gc6p',
    'XaOP',
    'VXXE',
    'XnCR',
    'aZrD'
]
const account = document.querySelector('.account')
const psw = document.querySelector('.psw')
const inner = document.querySelector('.inner')
const inner1 = document.querySelector('.inner1')
const reginner = document.querySelector('.reginner')
const reginner1 = document.querySelector('.reginner1')
const err = document.querySelector('.err')
let innerBol = false
let innerBol1 = false
let reginnerBol = false
let reginnerBol1 = false
let codeIpt
let num
let regNum
const line = document.querySelector('.line')
inner.onclick=()=>{
    if(!innerBol){
        inner.style.backgroundColor='#ff4c2f'
        innerBol=true
    }else{
        inner.style.backgroundColor='#fff'
        innerBol=false
    }
}
inner1.onclick=()=>{
    if(!innerBol1){
        inner1.style.backgroundColor='#ff4c2f'
        innerBol1=true
    }else{
        inner1.style.backgroundColor='#fff'
        innerBol1=false
    }
}
reginner.onclick=()=>{
    if(!reginnerBol){
        reginner.style.backgroundColor='#ff4c2f'
        reginnerBol=true
    }else{
        reginner.style.backgroundColor='#fff'
        reginnerBol=false
    }
}
reginner1.onclick=()=>{
    if(!reginnerBol1){
        reginner1.style.backgroundColor='#ff4c2f'
        reginnerBol1=true
    }else{
        reginner1.style.backgroundColor='#fff'
        reginnerBol1=false
    }
}
const codeChange=()=>{
    num = Math.floor(Math.random()*5)
    codeImg.innerHTML=`
        <input type="text" placeholder="验证码" class="code">
        <img onclick="codeChange()" src="${codeImgs[num]}" alt="">
    `
    codeIpt = document.querySelector('.verify input')
}
codeChange()
const test=()=>{
    let reg = new RegExp(`^${codeNum[num]}$`, 'i')
    let ipt1
    let ipt2
    if(innerBol1){
        ipt1=localStorage.getItem('account')
        ipt2=localStorage.getItem('psw')
    }else{
        ipt1=sessionStorage.getItem('account')
        ipt2=sessionStorage.getItem('psw')
    }
    console.log(ipt1)
    if(account.value==ipt1){
        if(psw.value==ipt2){
            if(reg.test(`${codeIpt.value}`)){
                    if(innerBol = true){
                    err.innerHTML=''
                        alert('登陆成功')
                        window.history.back()
                    }else{
                        err.innerHTML=`
                            请勾选用户协议
                        `
                    }
            }else{
                err.innerHTML=`
                    请输入正确的验证码
                `
            }
        }else{
            err.innerHTML=`
                请输入正确的密码
            `
        }
    }else{
        err.innerHTML=`
            请输入正确的账号
        `
    }
}
const register=()=>{
    let phoneReg = /^1[3-9]\d{9}$/
    let passwordReg = /^(?=.*[A-Za-z])(?=.*\d).{6,12}$/
    let regnumReg = new RegExp(`^${regNum}$`)
    const phone = document.querySelector('.regaccount')
    const regpsw = document.querySelector('.regpsw')
    console.log(regNum);
    
    if(phoneReg.test(`${phone.value}`)){
        if(passwordReg.test(`${regpsw.value}`)){
            if(regnumReg.test(`${regNum}`)&&regNum!=undefined){
                if(reginnerBol = true){
                    err.innerHTML=''
                    if(reginnerBol1){
                        alert("注册成功")
                        localStorage.setItem('account',phone.value)
                        localStorage.setItem('psw',regpsw.value)
                    }else{
                        alert("注册成功")
                        sessionStorage.setItem('account',phone.value)
                        sessionStorage.setItem('psw',regpsw.value)
                    }
                }else{
                    err.innerHTML=`
                        请勾选用户协议
                    `
                }
            }else{
                err.innerHTML=`
                    请输入正确的动态验证码
                `
            }
        }else{
            err.innerHTML=`
                请输入6-12位同时包含数字和字母的密码
            `
        }
    }else{
        err.innerHTML=`
            请输入正确的手机号
        `
    }
}
const changeTab=(t)=>{
    err.innerHTML=''
    const tab1=document.querySelector('.tab1')
    const tab2=document.querySelector('.tab2')
    const form = document.querySelector('.form')
    const register = document.querySelector('.register')
    tab1.classList.remove('active')
    tab2.classList.remove('active')
    t.classList.add('active')
    if(t.classList.contains('tab1')){
        line.style.left = '45px'
        form.style.display = 'block'
        register.style.display = 'none'
    }else{
        line.style.left = '220px'
        form.style.display = 'none'
        register.style.display = 'block'
    }
}
const sendPsw=()=>{
    regNum = String(Math.floor(Math.random()*10)) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10) + Math.floor(Math.random()*10)
    alert(regNum)
}